<template>
  <div>
    <div>
     <el-button size="mini" type="warning" @click="add()"><i class="el-icon-user-solid"></i></el-button>
    </div>
    <br>
    <el-table :data="powerList" style="width: 100%">
      <el-table-column label="编号">
        <template slot-scope="scope">
          <i class="el-icon-time"></i>
          <span style="margin-left: 10px">{{ scope.row.powerId }}</span>
        </template>
      </el-table-column>

      <el-table-column label="权限名">
        <template slot-scope="scope">
          <i class="el-icon-time"></i>
          <span style="margin-left: 10px">{{ scope.row.powerName }}</span>
        </template>
      </el-table-column>

      <el-table-column label="权限下标">
        <template slot-scope="scope">
          <i class="el-icon-time"></i>
          <span style="margin-left: 10px">{{ scope.row.powerIndex }}</span>
        </template>
      </el-table-column>

      <el-table-column label="权限地址">
        <template slot-scope="scope">
          <i class="el-icon-time"></i>
          <span style="margin-left: 10px">{{ scope.row.powerUrl }}</span>
        </template>
      </el-table-column>

      <el-table-column label="父节点">
        <template slot-scope="scope">
          <i class="el-icon-time"></i>
          <span style="margin-left: 10px">{{ scope.row.parentId }}</span>
        </template>
      </el-table-column>

      <el-table-column label="节点">
        <template slot-scope="scope">
          <i class="el-icon-time"></i>
          <span style="margin-left: 10px">{{ scope.row.nodeId }}</span>
        </template>
      </el-table-column>

      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button size="mini" type="success" @click="update(scope.row)">编辑</el-button>
          <el-button size="mini" type="danger" @click="delInfo(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <div class="block">
      <!--page-size默认每页显示条数-->
      <el-pagination @size-change="selectPage" @current-change="handleCurrentChange" :current-page="curPage"
        :page-sizes="[1, 3, 5, 8]" :page-size="rows" layout="total, sizes, prev, pager, next, jumper" :total="total">
      </el-pagination>
    </div>

    <!-- 对话框 -->
    <el-dialog :title="title" :visible.sync="isShow" width="30%">
      <el-form ref="form" label-width="80px">
        <br>
        <el-form-item label="用户名">
          <el-col :span="22">
            <input type="hidden" v-model="powerForm.powerId" />
            <el-input placeholder="请输入用户名" v-model="powerForm.powerName"></el-input>
          </el-col>
        </el-form-item>
      </el-form>

      <span slot="footer" class="dialog-footer">
        <el-button @click="isShow = false">取 消</el-button>
        <el-button type="primary" @click="saveInfo">保存</el-button>
      </span>
    </el-dialog>
  </div>

</template>
<script>
  export default {
    name: "powerListEle",
    data() {
      return {
        powerList: [],
        title: "新增", //模态框的标题
        powerForm: { //表单请求对象
          powerId: null,
          powerName: "",
          powerIndex: "",
          powerUrl: "",
          parentId: "",
          nodeId: ""
        },
        total: 0, //总条数
        totalPage: 0, //总页数
        curPage: 1, //当前页
        rows: 1, //每页显示条数,
        isShow: false
      }
    },
    methods: {
      loaddata() { //加载数据
        var mythis = this;
        $.ajax({
          url: "http://localhost:8080/power/list",
          type: "get",
          data: {
            "page": mythis.curPage,
            "row": mythis.rows
          },
          dataType: "json",
          success: function(data) {
            console.log(data)
            mythis.powerList = data.info;
            mythis.total = data.total;
            mythis.totalPage = data.totalPage
          }
        })
      },
      add() { //打开新增模态框
        //显示模态框
        $("#myModal").modal("show");
        this.isShow = true;
        //更改模态框的标题
        this.title = "新增";
        //清除表单元素
        this.powerForm.powerId = null;
        this.powerForm.powerName = "";
        this.powerForm.powerName = "";
        this.powerForm.powerUrl = "";
        this.powerForm.parentId = "";
        this.powerForm.nodeId = "";
      },
      update(x) { //打开修改模态框
        //显示模态框
        this.isShow = true;
        //更改模态框的标题
        this.title = "修改";
        console.log(x)
        //把选中的值赋值给模态框的表单元素
        this.powerForm.powerId = x.powerId;
        this.powerForm.powerName = x.powerName;
        this.powerForm.powerIndex = x.powerIndex;
        this.powerForm.powerUrl = x.powerUrl;
        this.powerForm.parentId = x.parentId;
        this.powerForm.nodeId = x.nodeId;
      },
      saveInfo() { //保存
        var url = ""
        if (this.title == "修改") {
          url = "http://localhost:8080/power/update"
        } else {
          url = "http://localhost:8080/power/add"
        }
        var mythis = this;
        $.ajax({
          url: url,
          type: "get",
          data: mythis.userForm,
          dataType: "json",
          success: function(data) {
            console.log(data)
            mythis.$message(data.info)
            if (data.info == "保存成功") {
              //关闭模态框
              mythis.isShow = false;
              //刷新
              mythis.loaddata();
            }
          }
        })
      },
      delInfo(x) { //删除
        var is = confirm("你确定要删除数据吗?");
        if (is) {
          var mythis = this;
          $.ajax({
            url: "http://localhost:8080/power/del",
            type: "get",
            data: {
              "id": x.roleId
            },
            dataType: "json",
            success: function(data) {
              console.log(data)
              mythis.$alert(data.info)
              if (data.info == "删除成功") {
                //刷新
                mythis.loaddata();
              }
            }
          })
        }
      },

      selectPage(v) { //用户选择页数
        this.rows = v;
        this.loaddata();
      },
      handleCurrentChange(v) { //当前页触发事件
        this.curPage = v;
        this.loaddata();
      }
    },
    mounted() {
      this.loaddata();
      console.log(this.$route.query.info)
    }

  }
</script>

<style>
</style>
